from django.db import models


# Create your models here.

class Host(models.Model):
    '''
    主机表
    '''
    host_status_choices = (
        (0, '正常'),
        (1, '异常'),
    )
    is_manage_choices = (
        (0, '已管理'),
        (1, '未管理'),
    )
    host_ip = models.CharField(max_length=16, verbose_name='主机 ip')
    host_name = models.CharField(max_length=64, verbose_name='主机名称')
    host_tag = models.ForeignKey(to='Host_tags', on_delete=models.DO_NOTHING, null=True,
                                 blank=True, verbose_name='主机标签', db_constraint=False)
    host_source = models.ForeignKey(to='Host_sources', on_delete=models.DO_NOTHING,
                                    null=True, blank=True, verbose_name='主机来源', db_constraint=False)
    host_OS = models.CharField(max_length=64, verbose_name='主机版本信息', null=True)
    host_CPU = models.SmallIntegerField(verbose_name='cpu 核数', null=True)
    host_RAM = models.CharField(max_length=16, verbose_name='内存值', null=True)
    host_disk = models.CharField(max_length=16, verbose_name='硬盘总空间', null=True)
    host_status = models.SmallIntegerField(choices=host_status_choices, default=0, verbose_name='主机状态')
    is_manage = models.SmallIntegerField(choices=is_manage_choices, default=0, verbose_name='管理状态')
    add_date = models.DateField(auto_now_add=True, verbose_name='添加时间')

    def host_status_name(self):
        return self.get_host_status_display()

    def is_manage_name(self):
        return self.get_is_manage_display()

    def host_tag_name(self):
        return self.host_tag.tag

    def host_source_name(self):
        return self.host_source.source

    def host_name_ip(self):
        return self.host_name + '_' + self.host_ip

    class Meta:
        db_table = 'ops_host'
        verbose_name = '主机表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.host_ip


class Host_tags(models.Model):
    '''
    主机标签表
    '''
    tag = models.CharField(max_length=16, verbose_name='主机标签')

    class Meta:
        db_table = 'ops_host_tags'
        verbose_name = '主机标签表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag


class Host_sources(models.Model):
    '''
    主机来源表
    '''
    source = models.CharField(max_length=16, verbose_name='主机来源')

    class Meta:
        db_table = 'ops_host_sources'
        verbose_name = '主机来源表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.source
